﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SQLite;

public partial class BasicInfo_DevInfo : UDCPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["dvid"]))
            {
                DeleteDev();
            }
            DevDataBind();
        }
    }

    private void DevDataBind()
    {
        string sqlstr = "select dvid,dvname from ud_developer_t where did = $did and ifvalid = 1";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$did",DbType.Int32)
                                };
        spc[0].Value = GetDid();
        DevList.DataSource = SQLiteHelper.ExecuteDataset(sqlstr, spc);
        DevList.DataBind();
    }
    protected void AddBtn_Click(object sender, EventArgs e)
    {
        if (IfExists())
        {
            msg.Text = "发展人已存在，请核实！";
            devTxT.Focus();
            return;
        }
        string sqlstr = "insert into ud_developer_t values(null,$dvname,$did,1);";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$dvname",DbType.String,40),
                                    new SQLiteParameter("$did",DbType.Int32)
                                };
        spc[0].Value = devTxT.Text.Trim();
        spc[1].Value = GetDid();
        SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        devTxT.Text = "";
        DevDataBind();
    }

    private bool IfExists()
    {
        string sqlstr = "select count(1) from ud_developer_t where dvname = $dvname and did = $did and ifvalid = 1";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$dvname",DbType.String,40),
                                    new SQLiteParameter("$did",DbType.Int32)
                                };
        spc[0].Value = devTxT.Text.Trim();
        spc[1].Value = GetDid();
        int count = Int32.Parse(SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString());
        return count > 0;
    }

    private bool DeleteDev()
    {
        string sqlstr = "update ud_developer_t set ifvalid = 0 where dvid = $dvid";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$dvid",DbType.Int32)
                                };
        spc[0].Value = Int32.Parse(Request.QueryString["dvid"].ToString());
        int count = SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        return count > 0;
    }
}